package offerv1;

import java.util.HashMap;
import java.util.Map;

public class Offer35 {
    //key-value 源链表-当前链表
    Map<Node, Node> cachedNode = new HashMap<Node, Node>();

    //1.hash表
    public Node copyRandomList(Node head) {
        //递归终止条件
        if (head == null) {
            return null;
        }

        if (!cachedNode.containsKey(head)) {
            Node node = new Node(head.val);
            cachedNode.put(head, node);//提前放入map 避免递归循环加载，类似spring
            node.next = copyRandomList(head.next);
            node.random = copyRandomList(head.random);
        }

        return cachedNode.get(head);
    }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
